package org.osflash.signals;

import asunit.asserts.*;
import org.osflash.signals.events.GenericEvent;
import flash.display.Sprite;

class SignalTest extends ISignalTestBase {

	@:meta(Before())
	public function setUp() : Void {
		signal = new Signal();
	}

	@:meta(Test())
	public function dispatch_should_pass_event_to_listener_but_not_set_signal_or_target_properties() : Void {
		signal.add(async.add(checkGenericEvent, 10));
		signal.dispatch(new GenericEvent());
	}

	function checkGenericEvent(e : GenericEvent) : Void {
		assertNull("event.signal is not set by Signal", e.signal);
		assertNull("event.target is not set by Signal", e.target);
	}

	@:meta(Test())
	public function dispatch_non_IEvent_without_error() : Void {
		signal.addOnce(checkSprite);
		// Sprite doesn't have a target property,
		// so if the signal tried to set .target,
		// an error would be thrown and this test would fail.
		signal.dispatch(new Sprite());
	}

	function checkSprite(sprite : Sprite) : Void {
		assertTrue(Std.is(sprite, Sprite));
	}

	@:meta(Test())
	public function adding_dispatch_method_as_listener_does_not_throw_error() : Void {
		var redispatchSignal : Signal = new Signal(GenericEvent);
		signal = new Signal(GenericEvent);
		signal.add(redispatchSignal.dispatch);
	}

	@:meta(Test())
	public function slot_params_should_be_sent_through_to_listener() : Void {
		var listener : Dynamic = function(number : Int, string : String, sprite : Sprite) : Void {
			assertEquals(number, 12345);
			assertEquals(string, "text");
			assertEquals(sprite, slot.params[2]);
		}
;
		var slot : ISlot = signal.add(async.add(listener));
		slot.params = [12345, "text", new Sprite()];
		signal.dispatch();
	}

	@:meta(Test())
	public function slot_params_with_with_10_params_should_be_sent_through_to_listener() : Void {
		// Test the function.apply - maying sure we get everything we ask for.
		var listener : Dynamic = function(number : Int, string : String, sprite : Sprite, alpha0 : String, alpha1 : String, alpha2 : String, alpha3 : String, alpha4 : String, alpha5 : String, alpha6 : String) : Void {
			assertEquals(number, 12345);
			assertEquals(string, "text");
			assertEquals(sprite, slot.params[2]);
			assertEquals(alpha0, "a");
			assertEquals(alpha1, "b");
			assertEquals(alpha2, "c");
			assertEquals(alpha3, "d");
			assertEquals(alpha4, "e");
			assertEquals(alpha5, "f");
			assertEquals(alpha6, "g");
		}
;
		var slot : ISlot = signal.add(async.add(listener));
		slot.params = [12345, "text", new Sprite(), "a", "b", "c", "d", "e", "f", "g"];
		signal.dispatch();
	}

}

